package com.android.hedgehog;

import java.util.ArrayList;

import com.android.hedgehog.framework.Graphics;

import android.graphics.Color;

public class Keyboard2 {
	public String output = "";
	String LETTERS = "QWERTYUIOPASDFGHJKLZXCVBNM-";
	String NUMBERS = "1234567890";
	Letter[] letters;
	Letter[] numbers;
	int inputLimit;
	final int keyWidth = 27;
	final int keyHeight = 38;
	ArrayList<Letter> clickedLetters = new ArrayList<Letter>();
	public boolean lettersOn;
	public boolean numbersOn;
	Graphics graphics;
	int delYpos;
	final int horizontalGap = 32;
	final int fontStyle = 2;
	final int fontSize = 20;
	final int strokeWidth = 1;
	final int backgroundColor = Color.GRAY;
	final int borderColor = Color.DKGRAY;
	final int textColor = Color.WHITE;

	public Keyboard2(Graphics graphics) {
		this.graphics = graphics;
		setKeyboard();
		setMode(15, true, true, false); // default: full keyboard
	}

	/**
	 * This method is run once right before using keyboard. and run again after
	 * each time you want to change the settings of the keyboard
	 * 
	 * @param inputSizeLimit
	 * @param lettersOn
	 * @param numbersOn
	 * @param clear
	 *            clears previously typed letters
	 */
	public void setMode(int inputSizeLimit, boolean lettersOn, boolean numbersOn, boolean clear) {
		inputLimit = inputSizeLimit;
		this.lettersOn = lettersOn;
		this.numbersOn = numbersOn;
		if (lettersOn) {
			delYpos = 390;
		} else {
			delYpos = 300;
		}
		if (clear) {
			clearOutput();
		}
	}

	private void setKeyboard() {

		letters = new Letter[LETTERS.length()];

		for (int i = 0; i < 10; i++) {
			letters[i] = new Letter(LETTERS.charAt(i));
			letters[i].setPosition(3 + (i * horizontalGap), 300, keyWidth, keyHeight);
		}
		for (int i = 0; i < 9; i++) {
			letters[i + 10] = new Letter(LETTERS.charAt(i + 10));
			letters[i + 10].setPosition(15 + (i * horizontalGap), 345, keyWidth, keyHeight);
		}
		for (int i = 0; i < 8; i++) {
			letters[i + 19] = new Letter(LETTERS.charAt(i + 19));
			letters[i + 19].setPosition(28 + (i * horizontalGap), 390, keyWidth, keyHeight);
		}

		numbers = new Letter[NUMBERS.length()];

		for (int i = 0; i < NUMBERS.length(); i++) {
			numbers[i] = new Letter(NUMBERS.charAt(i));
			numbers[i].setPosition(3 + (i * horizontalGap), 255, keyWidth, keyHeight);
		}
	}

	public void click(int x, int y) {

		if (clickedLetters.size() < inputLimit) {

			if (lettersOn) {// TOUCH LETTERS
				for (int i = 0; i < letters.length; i++) {
					if (x > letters[i].x && x < (letters[i].x + horizontalGap) && y > letters[i].y
							&& y < (letters[i].y + letters[i].height))
						clickedLetters.add(letters[i]);
				}
			}// TOUCH LETTERS

			if (numbersOn) {// TOUCH NUMBERS
				for (int i = 0; i < numbers.length; i++) {
					if (x > numbers[i].x && x < (numbers[i].x + horizontalGap) && y > numbers[i].y
							&& y < (numbers[i].y + numbers[i].height))
						clickedLetters.add(numbers[i]);
				}
			}// TOUCH NUMBERS

		}

		if (x > 284 && y > delYpos && y < delYpos + keyHeight) { // DELETE
			if (clickedLetters.size() > 0) {
				clickedLetters.remove(clickedLetters.size() - 1);
			}
		} // DELETE

		adjustOutput();
	}

	public void display() {

		if (lettersOn) {
			for (int i = 0; i < letters.length; i++) {
				graphics.drawUniversalRect(letters[i].x, letters[i].y, letters[i].width, letters[i].height,
						false, true, 1, 255, backgroundColor);
				graphics.drawUniversalRect(letters[i].x, letters[i].y, letters[i].width, letters[i].height,
						false, false, strokeWidth, 255, borderColor);
				graphics.drawUniversalText("" + letters[i].letter, fontStyle, true, letters[i].letterX,
						letters[i].letterY, fontSize, 255, textColor);
			}
		}

		if (numbersOn) {
			for (int i = 0; i < numbers.length; i++) {
				graphics.drawUniversalRect(numbers[i].x, numbers[i].y, numbers[i].width, numbers[i].height,
						false, true, 1, 255, backgroundColor);
				graphics.drawUniversalRect(numbers[i].x, numbers[i].y, numbers[i].width, numbers[i].height,
						false, false, strokeWidth, 255, borderColor);
				graphics.drawUniversalText("" + numbers[i].letter, fontStyle, true, numbers[i].letterX,
						numbers[i].letterY, fontSize, 255, textColor);
			}
		}

		graphics.drawUniversalRect(284, delYpos, keyWidth + 9, keyHeight, false, true, 1, 255, Color.GRAY);
		graphics.drawUniversalRect(284, delYpos, keyWidth + 9, keyHeight, false, false, 2, 255, Color.DKGRAY);
		graphics.drawUniversalText("DEL", 2, true, 302, delYpos + 27, 16, 255, Color.RED);

	}

	private void adjustOutput() {
		output = "";
		for (int i = 0; i < clickedLetters.size(); i++) {
			output += clickedLetters.get(i).letter;
		}
	}

	public void clearOutput() {
		clickedLetters.clear();
		adjustOutput();
	}

}
